import { baseRequest } from '../common/axios'
import axios from 'axios'
import appConfig from '@/static/app.config.js'
import store from '@/storeVuex/index'
const request = (url, ...arg) => baseRequest(`/api/WeChat/Common/${url}`, ...arg)

export default {
	GetCountyInfos(data) {
		return request('GetCountyInfos', data, 'get')
	},
	GetLegendList(data) {
		return baseRequest('/files/unit/legend_qzwx1.json?t=' + new Date().getTime(), data, 'get')
	},
	// 省服接口
	service(data) {
		return axios({
			method: 'post',
			url: '/api/ztq30_fj_jc/service.do',
			data,
			responseEncoding: 'utf8',
			headers: {
				'Content-Type': 'multipart/json;charset=UTF8'
				// 'X-Requested-With': 'XMLHttpRequest'
				// 'content-Type': 'application/x-www-form-urlencoded'
			}
		})
	},
	geoserver(data) {
		return baseRequest(appConfig['geoserver'] + '/geoserver/sanming/wms', data, 'get')
	},
	async getCity(data) {
		// 废弃
		// return baseRequest(
		// 	appConfig['geoserver'] +
		// 		'/geoserver/sanming/wms?service=WFS&request=GetFeature&version=1.0.0&outputFormat=application/json&maxFeatures=10000&typeName=sanming:shi',
		// 	data,
		// 	'get'
		// )
		let shi = await store.dispatch('getDataByKey', { key: 'shiJson' })
		if (shi) {
			return shi
		} else {
			let params = {
				service: 'WFS',
				request: 'GetFeature',
				version: '1.0.0',
				outputFormat: 'application/json',
				maxFeatures: '10000',
				typeName: 'sanming:shi'
			}
			shi = await baseRequest(appConfig['geoserver'] + '/geoserver/sanming/wms', params, 'get')
			// shi = await baseRequest('/data/geojson/shi.json', {}, 'get')
			store.dispatch('getDataByKey', { key: 'shiJson', data: shi })
			return shi
		}
		// return baseRequest('/data/geojson/shi.json', {}, 'get')
	},
	getJson(url) {
		return baseRequest(url, {}, 'get')
	},
	async getCounty(data) {
		let xian = await store.dispatch('getDataByKey', { key: 'xianJson' })
		if (xian) {
			return xian
		} else {
			let params = {
				service: 'WFS',
				request: 'GetFeature',
				version: '1.0.0',
				outputFormat: 'application/json',
				maxFeatures: '10000',
				typeName: 'sanming:xian'
			}
			xian = await baseRequest(appConfig['geoserver'] + '/geoserver/sanming/wms', params, 'get')
			store.dispatch('getDataByKey', { key: 'xianJson', data: xian })
			return xian
		}
	},
	async getTown(data) {
		// 废弃
		// return baseRequest(
		// 	appConfig['geoserver'] +
		// 		'/geoserver/sanming/wms?service=WFS&request=GetFeature&version=1.0.0&outputFormat=application/json&maxFeatures=10000&typeName=sanming:xiang',
		// 	data,
		// 	'get'
		// )
		let xiangJson = await store.dispatch('getDataByKey', { key: 'xiangJson' })
		if (xiangJson) {
			return xiangJson
		} else {
			let params = {
				service: 'WFS',
				request: 'GetFeature',
				version: '1.0.0',
				outputFormat: 'application/json',
				maxFeatures: '10000',
				typeName: 'sanming:xiang'
			}
			xiangJson = await baseRequest(appConfig['geoserver'] + '/geoserver/sanming/wms', params, 'get')
			// xiangJson = await baseRequest('/data/geojson/xiang.json', {}, 'get')
			store.dispatch('getDataByKey', { key: 'xiangJson', data: xiangJson })
			return xiangJson
		}
		// return baseRequest('/data/geojson/xiang.json', {}, 'get')
	},
	onlineuser(data) {
		return baseRequest('/api/smsl/biz/onlineuser/list', data, 'get')
	},
	updateOrAdd(data) {
		return baseRequest('/api/smsl/biz/onlineuser/updateOrAdd', data, 'post')
	},
	getVersion(params) {
		return baseRequest('/package/version.json', params, 'get')
	},
	getGeocoder(lat, lon) {
		return baseRequest(
			'https://qz.qzqxw.com/api/geocoder/v1',
			{
				location: `${lon},${lat}`
			},
			'get'
		)
	}
}
